﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForms
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
        }
        #region 确定
        private void button1_Click(object sender, EventArgs e)
        {
            //获取数据
            string a = uid.Text;
            string b = this.pwd.Text;
            string c = surepwd.Text;

            //正则表达式
            string email = "^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$";
            Regex regex = new Regex(email);

            string phone = @"^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$";
            Regex regex1 = new Regex(phone);

            string id = @"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$";
            Regex regex2 = new Regex(id);

            string name = @"^[\u4e00-\u9fa5.·]{0,}$";
            Regex regex3 = new Regex(name);

            string user = @"^[a-zA-Z0-9_]{3,16}$";
            Regex regex4 = new Regex(user);

            string pwd = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$";
            Regex regex5 = new Regex(pwd);
            //判断输入内容是否符合规则
            if (uid.Text == string.Empty || this.pwd.Text == string.Empty || surepwd.Text == string.Empty || this.email.Text == string.Empty || this.phone.Text == string.Empty || this.name.Text == string.Empty || cardid.Text == string.Empty)
            {
                MessageBox.Show("注册信息不能为空！", "提示");
            } 
            //判断输入的内容是否符合正则表达式
            else if (b.Equals(c) && regex2.IsMatch(cardid.Text) && regex3.IsMatch(this.name.Text) && regex1.IsMatch(this.phone.Text) && regex.IsMatch(this.email.Text) && regex5.IsMatch(this.pwd.Text) && regex4.IsMatch(uid.Text))
            {
                string sqllogin = "server=LAPTOP-AS39K086\\MSSQLSERVER1;uid=HUANG;pwd=571193002;database=WinFrom ";
                SqlConnection sqlConnection = new SqlConnection(sqllogin);
                sqlConnection.Open();
                string commed = "insert into Login (phone,pwd,email) values ('" + this.phone.Text + "','" + this.pwd.Text + "','" + this.email.Text + "')";
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(commed, sqlConnection);
                DataTable dataTable = new DataTable();
                sqlDataAdapter.Fill(dataTable);
                MessageBox.Show("注册成功！", "提示");
                this.Close();
            }
            else if (!regex2.IsMatch(cardid.Text))
            {
                MessageBox.Show("请输入正确的身份证", "提示");
            }
            else if (!regex3.IsMatch(this.name.Text))
            {
                MessageBox.Show("请输入正确的姓名", "提示");
            }
            else if (!regex1.IsMatch(this.phone.Text))
            {
                MessageBox.Show("请输入正确的电话", "提示");
            }
            else if (!regex.IsMatch(this.email.Text))
            {
                MessageBox.Show("请输入正确的邮箱", "提示");
            }
            else if (!regex5.IsMatch(this.pwd.Text))
            {
                MessageBox.Show("密码至少包含：数字和英文，长度6-20", "提示");
            }
            else if (!regex4.IsMatch(uid.Text))
            {
                MessageBox.Show("用户名含有敏感字眼", "提示");
            }
            else
            {
                MessageBox.Show("确认密码需与密码一致", "提示");
            }
        }
        #endregion

        #region 取消
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
}
